package com.fernfx.xingtan.main.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseFragment;
import com.fernfx.xingtan.common.photo.RobredPacketHistroyAdapter;
import com.fernfx.xingtan.main.contract.RobredPacketHistroyContract;
import com.fernfx.xingtan.main.entity.RobredPacketHistroyEntity;
import com.fernfx.xingtan.main.presenter.RobredPacketHistoryPresenter;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.ToastUtil;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by admin on 2018/8/10.
 * 发红包记录
 */

public class RobredPacketHistoryFragment extends BaseFragment implements RobredPacketHistroyContract.View {
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.robred_packet_rlt)
    TwinklingRefreshLayout robredPacketRlt;
    @BindView(R.id.robred_packet_histroy_liv)
    ListView robredPacketHistroyLiv;

    private int startIndex = 1;
    private int size = 20;
    //presenter
    private RobredPacketHistroyContract.Presenter presenter = new RobredPacketHistoryPresenter();
    private RobredPacketHistroyAdapter adapter;
    //data list
    List<RobredPacketHistroyEntity.ObjBean.RecordsBean> dataList;
    //是否可以加载下一页
    private boolean isLoadMore,loadFlag;
    //url
    public static final String REQUEST_URL = "request_url";
    //request args
    private String url,orderBy;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_robred_packet_send_histroy;
    }

    @Override
    protected void initData() {
        //init
        presenter.init(this);
        Bundle bundle = getArguments();
        RobredPacketHistroyActivity.FragmentArgsEntity fragmentArgsEntity = (RobredPacketHistroyActivity.FragmentArgsEntity) bundle.getSerializable(REQUEST_URL);
        url = fragmentArgsEntity.url;
        orderBy = fragmentArgsEntity.orderBy;
        request();
    }

    @Override
    protected void initView() {
        dataList = new ArrayList<>(0);
        adapter = new RobredPacketHistroyAdapter(getActivity(), dataList);
        robredPacketHistroyLiv.setAdapter(adapter);
    }

    @Override
    public void showRobredPacketHistroy(List<RobredPacketHistroyEntity.ObjBean.RecordsBean> records) {
        if (loadFlag) {
            dataList.clear();
            robredPacketRlt.finishRefreshing();
        } else {
            robredPacketRlt.finishLoadmore();
        }
        if (CollectionUtil.isEmpty(records)) {
            noDataTv.setVisibility(View.VISIBLE);
            robredPacketRlt.setVisibility(View.GONE);
        } else {
            noDataTv.setVisibility(View.GONE);
            robredPacketRlt.setVisibility(View.VISIBLE);
            //刷新adapter
            dataList.clear();
            dataList.addAll(records);
            adapter.notifyDataSetChanged();
            //上拉刷新和下拉加载
            robredPacketRlt.setOnRefreshListener(new RefreshListenerAdapter() {
                public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                    startIndex = 1;
                    size = 10;
                    loadFlag = true;
                    //允许继续下一页
                    isLoadMore = true;
                    request();
                }

                public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                    if (!isLoadMore) {
                        ToastUtil.showCentertoast("已是最后一页");
                        robredPacketRlt.finishLoadmore();
                        return;
                    }
                    loadFlag = false;
                    startIndex += 1;
                    size = 10;
                    request();
                }
            });
            if (records.size() < 10) {
                isLoadMore = false;
            }
        }
    }

    public void request() {
        //request
        requestArgsMap.clear();
        requestArgsMap.put("pageNo", startIndex);
        requestArgsMap.put("pageSize", size);
        requestArgsMap.put("orderBy", orderBy);
        requestArgsMap.put("order", "desc");
        presenter.sendRobredPacketHistroy(requestArgsMap,url);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!CollectionUtil.isEmpty(requestArgsMap)) {
            requestArgsMap.clear();
        }
        if (!CollectionUtil.isEmpty(dataList)) {
            dataList.clear();
            dataList = null;
        }
        if (presenter != null) {
            presenter = null;
        }
        if (adapter != null) {
            adapter = null;
        }
    }
}
